<template>
  <div>
    <el-form
      ref="queryCondition"
      :model="searchForm.queryCondition"
      label-width="auto"
    >
      <el-row :gutter="12">
        <el-col :span="4" :offset="1">
          <el-form-item prop="" label="名称">
            <el-input v-model.trim="searchForm.queryCondition.nickNameSml">
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item prop="" label="类别">
            <el-select v-model="searchForm.queryCondition.professions" multiple>
              <template v-for="(e, i) in professionsEnums">
                <el-option :key="i" :value="e.value" :label="e.text">
                </el-option>
              </template>
            </el-select>
          </el-form-item>
        </el-col>

        <el-col :span="4">
          <el-form-item prop="" label="最小值">
            <el-select v-model="searchForm.queryCondition.goldCoinMin">
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="4">
          <el-form-item prop="" label="最大值">
            <el-select v-model="searchForm.queryCondition.goldCoinMax">
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="2">
          <el-form-item label-width="10px">
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="queryByCondition(1)"
              >查询
            </el-button>
          </el-form-item>
        </el-col>
        <el-col :span="1">
          <el-form-item label-width="0">
            <el-button
              type="warning"
              icon="el-icon-refresh"
              @click="() => $refs.queryCondition.resetFields()"
            >
            </el-button>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <player-insert @postSubmit="queryByCondition"></player-insert>
    <el-table
      :data="results"
      ref="resultsTable"
      height="480px"
      stripe
      highlight-current-row
      @row-click="rowClick"
      @selection-change="selectionChange"
    >
      <el-table-column type="selection" align="center"></el-table-column>

      <el-table-column type="expand">
        <template #default="{ row }">
          <el-descriptions border>
            <el-descriptions-item label="填充1">具体信息1</el-descriptions-item>
            <el-descriptions-item label="playerId">{{
              row.playerId
            }}</el-descriptions-item>
          </el-descriptions>
        </template>
      </el-table-column>

      <el-table-column prop="playerId" label="ID" width="40"></el-table-column>
      <el-table-column
        prop="playerCode"
        label="Code"
        width="80"
      ></el-table-column>
      <el-table-column
        prop="nickname"
        label="昵称"
        width="120"
      ></el-table-column>
      <el-table-column
        prop="goldCoin"
        label="金币"
        width="80"
      ></el-table-column>
      <el-table-column
        prop="silverCoin"
        label="银币"
        width="80"
      ></el-table-column>

      <el-table-column
        width="200"
        label="profession"
        :formatter="professionFormat"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="usernameFk"
        label="外键"
        width="120"
      ></el-table-column>
      <el-table-column width="180px" fixed="left">
        <template #default="{ row }">
          <el-button
            size="mini"
            type="warning"
            icon="el-icon-edit"
            @click="openById(row.playerId)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-edit"
            @click="deleteById(row.playerId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :total="total"
      :page-size="searchForm.pageSize"
      :current-page.sync="searchForm.pageNum"
      @current-change="queryByCondition"
      layout="total, prev, pager, next, jumper"
    ></el-pagination>
    <div
      ref="show"
      style="width: 600px; height: 500px; background-color: #b6aa9a"
    ></div>
  </div>
</template>
<script>
import PlayerInsert from "@/components/PlayerInsert";
/**
 * 页面核心数据结构体说明文档
 * searchForm: {
        pageNum: 1,
        pageSize: 10,
        queryCondition: {
          goldCoinMax: 0,
          goldCoinMin: 0,
          nickNameSml: "",
          professions: ["architect"],
        },
      },
 };
 *
 * http://localhost:9090/ceto/player/query
 *
 * {
      "playerId": 8,
      "playerCode": "8110794",
      "nickname": "李苏东",
      "goldCoin": 80,
      "silverCoin": 92,
      "profession": "architect",
      "usernameFk": "2016207068",
      "version": 0
    },
 */
// import New from './components/New.vue';
const professionsEnums = [
  {
    index: 0,
    text: "建筑师",
    value: "architect",
  },
  {
    index: 1,
    text: "会计师",
    value: "accountant",
  },
  {
    index: 2,
    text: "巫师",
    value: "wizard",
  },
  {
    index: 3,
    text: "作家",
    value: "writer",
  },
];
export default {
  name: "FinalPlayer",
  components: {
    PlayerInsert,
    // New,Old,
  },
  props: {
    sampleP: {
      type: Number,
      default: 0,
    },
  },
  data() {
    return {
      sampleD: "value",
      searchForm: {
        pageNum: 1,
        pageSize: 10,
        queryCondition: {
          goldCoinMax: 0,
          goldCoinMin: 0,
          nickNameSml: "",
          professions: ["architect"],
        },
      },
      professionsEnums: professionsEnums,
      results: [],
      total: 0,
    };
  },
  computed: {
    // sampleC() {
    //     return this.sampleD+'--';
    // }
  },
  created() {},
  mounted() {
    this.queryByCondition(1);
    this.showInit();
  },
  methods: {
    sampleM() {},
    showInit() {
      let e = this.$echarts.init(this.$refs.show);
      e.setOption({
        title: {
          text: "统计区间",
        },
        xAxis: {
          type: "category",
          data: ["<60", "60-80", "80-100"],
        },
        yAxis: {
          type: "value",
        },
        series: [
          { name: "gold", type: "bar", data: [10, 20, 30] },
          { name: "silver", type: "bar", data: [20, 30, 10] },
        ],
      });
    },
    showRender() {
      let goldCount = Array.of(0, 0, 0);
      let silverCount = Array.of(0, 0, 0);
      for (let row of this.results) {
        if (row.silverCoin <= 60) {
          silverCount[0]++;
        } else if (row.silverCoin <= 80) {
          silverCount[1]++;
        } else {
          silverCount[2]++;
        }
        if (row.goldCoin <= 60) {
          goldCount[0]++;
        } else if (row.goldCoin <= 80) {
          goldCount[1]++;
        } else {
          goldCount[2]++;
        }
      }
      console.log(goldCount, silverCount);
      let p = this.$echarts.getInstanceByDom(this.$refs.show);
      p.setOption({
        series: [
          {
            name: "gold",
            data: goldCount,
          },
          {
            name: "silver",
            data: silverCount,
          },
        ],
      });
    },
    queryByCondition(pageNum) {
      if (pageNum) this.searchForm.pageNum = pageNum;
      //todo: arrange into apis

      this.$http({
        method: "post",
        url: "http://localhost:9090/ceto/player/query",
        data: this.searchForm,
      }).then(
        (res) => {
          this.total = res.data.total;
          this.results = res.data.results;
          this.showRender();
        },
        (error) => {
          this.$message.warning("query error: " + error.response.status);
        }
      );
    },
    professionFormat(row) {
      // row.profession;
      let s = this.professionsEnums.find((x) => x.value === row.profession);
      return s ? s.text : "";
    },
  },
  watch: {
    // sampleC(newValue, oldValue) {
    //
    // }
  },
};
</script>

<!--<style scoped>-->
<style lang="scss" scoped></style>
